ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

Integrate Atlas Vector Search with LangChain

You can integrate Atlas Vector Search with LangChain to build generative AI and RAG applications. This page provides an overview of the MongoDB LangChain Python integration and the different components you can use in your applications.

Get Started

Note

For a full list of components and methods, see API reference.

For the JavaScript integration, see Get Started with the LangChain JS/TS Integration.

To use Atlas Vector Search with LangChain, you must first install the langchain-mongodb package:

pip install langchain-mongodb

Some components also require the following LangChain base packages:

pip install langchain langchain_community

MongoDBAtlasVectorSearch is a vector store that allows you to store and retrieve vector embeddings from a collection in Atlas. You can use this component to store embeddings from your data and retrieve them using Atlas Vector Search.

This component requires an Atlas Vector Search Index.

from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient
# Use some embedding model to generate embeddings
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings
# Connect to your Atlas cluster
client = MongoClient("<connection-string>")
collection = client["<database-name>"]["<collection-name>"]
# Instantiate the vector store
vector_store = MongoDBAtlasVectorSearch(
collection = collection, # Collection to store embeddings
embedding = FakeEmbeddings(), # Embedding model to use
index_name = "vector_index", # Name of the vector search index
relevance_score_fn = "cosine" # Similarity score function, can also be "euclidean" or "dotProduct"
)

LangChain retrievers are components that you use to get relevant documents from your vector stores. You can use LangChain's built-in retrievers or the following MongoDB retrievers to query and retrieve data from Atlas.

MongoDBAtlasFullTextSearchRetriever is a retriever that performs full-text search by using Atlas Search. Specifically, it uses Lucene's standard BM25 algorithm.

This retriever requires an Atlas Search Index.

from langchain_mongodb.retrievers.full_text_search import MongoDBAtlasFullTextSearchRetriever
# Connect to your Atlas cluster
client = MongoClient("<connection-string>")
collection = client["<database-name>"]["<collection-name>"]
# Initialize the retriever
retriever = MongoDBAtlasFullTextSearchRetriever(
collection = collection, # MongoDB Collection in Atlas
search_field = "<field-name>", # Name of the field to search
search_index_name = "<index-name>" # Name of the search index
)
# Define your query
query = "some search query"
# Print results
documents = retriever.invoke(query)
for doc in documents:
print(doc)

MongoDBAtlasHybridSearchRetriever is a retriever that combines vector search and full-text search results by using the Reciprocal Rank Fusion (RRF) algorithm. To learn more, see How to Perform Hybrid Search.

This retriever requires an existing vector store, Atlas Vector Search Index, and Atlas Search Index.

from langchain_mongodb.retrievers.hybrid_search import MongoDBAtlasHybridSearchRetriever
# Initialize the retriever
retriever = MongoDBAtlasHybridSearchRetriever(
vectorstore = <vector-store>, # Vector store instance
search_index_name = "<index-name>", # Name of the Atlas Search index
top_k = 5, # Number of documents to return
fulltext_penalty = 60.0, # Penalty for full-text search
vector_penalty = 60.0 # Penalty for vector search
)
# Define your query
query = "some search query"
# Print results
documents = retriever.invoke(query)
for doc in documents:
print(doc)

MongoDBAtlasParentDocumentRetriever is a retriever that queries smaller chunks first and then returns the larger parent document to the LLM. This type of retrieval is called parent document retrieval. Parent document retrieval can improve the responses of your RAG agents and applications by allowing for more granular searches on smaller chunks while giving LLMs the full context of the parent document.

This retriever stores both the parent and child documents in a single MongoDB collection, which supports efficient retrieval by only having to compute and index the child documents' embeddings.

Under the hood, this retriever creates the following:

from langchain_mongodb.retrievers.parent_document import ParentDocumentRetriever
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
retriever = MongoDBAtlasParentDocumentRetriever.from_connection_string(
connection_string = <connection-string>, # Atlas connection string
embedding_model = OpenAIEmbeddings(), # Embedding model to use
child_splitter = RecursiveCharacterTextSplitter(), # Text splitter to use
database_name = <database-name>, # Database to store the collection
collection_name = <collection-name>, # Collection to store the collection
# Additional vector store or parent class arguments...
)
# Define your query
query = "some search query"
# Print results
documents = retriever.invoke(query)
for doc in documents:
print(doc)

Caches are used to optimize LLM performance by storing repetitive responses for similar or repetitive queries to avoid recomputing them. MongoDB provides the following caches for your LangChain applications.

MongoDBCache allows you to store a basic cache in Atlas.

from langchain_mongodb import MongoDBCache
from langchain_core.globals import set_llm_cache
set_llm_cache(MongoDBCache(
connection_string = "<connection-string>", # Atlas connection string
database_name = "<database-name>", # Database to store the cache
collection_name = "<collection-name>" # Collection to store the cache
))

Semantic caching is a more advanced form of caching that retrieves cached prompts based on the semantic similarity between the user input and cached results.

MongoDBAtlasSemanticCache is a semantic cache that uses Atlas Vector Search to retrieve the cached prompts. This component requires an Atlas Vector Search index.

from langchain_mongodb import MongoDBAtlasSemanticCache
from langchain_core.globals import set_llm_cache
# Use some embedding model to generate embeddings
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings
set_llm_cache(MongoDBAtlasSemanticCache(
embedding = FakeEmbeddings(), # Embedding model to use
connection_string = "<connection-string>", # Atlas connection string
database_name = "<database-name>", # Database to store the cache
collection_name = "<collection-name>" # Collection to store the cache
))

Document loaders are tools that help you to load data for your LangChain applications.

MongodbLoader is a document loader that returns a list of documents from a MongoDB database.

from langchain_community.document_loaders.mongodb import MongodbLoader
loader = MongodbLoader(
connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI
db_name = "<database-name>", # Database that contains the collection
collection_name = "<collection-name>", # Collection to load documents from
filter_criteria = { <filter-document> }, # Optional document to specify a filter
field_names = ["<field-name>", ... ] # List of fields to return
)
docs = loader.load()

MongoDBChatMessageHistory is a component that allows you to store and manage chat message histories in a MongoDB database. It can save both user and AI-generated messages associated with a unique session identifier. This is useful for applications that require tracking of interactions over time, such as chatbots.

from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
chat_message_history = MongoDBChatMessageHistory(
session_id = "<session-id>", # Unique session identifier
connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI
database_name = "<database-name>", # Database to store the chat history
collection_name = "<collection-name>" # Collection to store the chat history
)
chat_message_history.add_user_message("Hello")
chat_message_history.add_ai_message("Hi")
chat_message_history.messages
[HumanMessage(content='Hello'), AIMessage(content='Hi')]

You can use the following custom data stores to manage and store data in MongoDB.

MongoDBDocStore is a custom key-value store that uses MongoDB to store and manage documents. You can perform CRUD operations as you would on any other MongoDB collection.

from pymongo import MongoClient
from langchain_core.documents import Document
from langchain_mongodb.docstores import MongoDBDocStore
# Replace with your MongoDB connection string and namespace
connection_string = "<connection-string>"
namespace = "<database-name>.<collection-name>"
# Initialize the MongoDBDocStore
docstore = MongoDBDocStore.from_connection_string(connection_string, namespace)

MongoDBByteStore is a custom datastore that uses MongoDB to store and manage binary data, specifically data represented in bytes. You can perform CRUD operations with key-value pairs where the keys are strings and the values are byte sequences.

from langchain.storage import MongoDBByteStore
# Instantiate the MongoDBByteStore
mongodb_store = MongoDBByteStore(
connection_string = "<connection-string>", # Atlas cluster or local MongoDB instance URI
db_name = "<database-name>", # Name of the database
collection_name = "<collection-name>" # Name of the collection
)
# Set values for keys
mongodb_store.mset([("key1", b"hello"), ("key2", b"world")])
# Get values for keys
values = mongodb_store.mget(["key1", "key2"])
print(values) # Output: [b'hello', b'world']
# Iterate over keys
for key in mongodb_store.yield_keys():
print(key) # Output: key1, key2
# Delete keys
mongodb_store.mdelete(["key1", "key2"])

To learn how to integrate Atlas Vector Search with LangGraph, see Integrate MongoDB with LangGraph.

MongoDB also provides the following developer resources: